﻿/**
 * Pagination.java 1.0 2007-4-25 上午04:13:50,创建 by yangfan
 */


package org.in4j.core.dao.support;

import java.util.List;

/**
 * @author yangfan
 * @version 2007-4-25 上午04:13:50,创建
 */
public class Pagination1<T>
{

	public final static int PAGESIZE = 25;

	private int pageSize = PAGESIZE;

	private List<T> items;

	private int recordCount;

	private int[] indexes = new int[0];

	private int startIndex = 0;

//	private int pageNo;

	public Pagination1(List<T> items, int recordCount)
	{
		setPageSize(PAGESIZE);
		setRecordCount(recordCount);
		setItems(items);
		setStartIndex(0);
	}

	public Pagination1(List<T> items, int totalCount, int startIndex)
	{
		setPageSize(PAGESIZE);
		setRecordCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public Pagination1(List<T> items, int totalCount, int startIndex, int pageSize)
	{
		setPageSize(pageSize);
		setRecordCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public List<T> getItems()
	{
		return items;
	}

	public void setItems(List<T> items)
	{
		this.items = items;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getRecordCount()
	{
		return recordCount;
	}

	public void setRecordCount(int totalCount)
	{
		if (totalCount > 0)
		{
			this.recordCount = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				count++;
			indexes = new int[count];
			for(int i = 0; i < count; i++)
			{
				indexes[i] = pageSize * i;
			}
		}
		else
		{
			this.recordCount = 0;
		}
	}

	public int[] getIndexes()
	{
		return indexes;
	}

	public void setIndexes(int[] indexes)
	{
		this.indexes = indexes;
	}

	public int getStartIndex()
	{
		return startIndex;
	}

	public void setStartIndex(int startIndex)
	{
		if (recordCount <= 0)
			this.startIndex = 0;
		else if (startIndex >= recordCount)
			this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0)
			this.startIndex = 0;
		else
		{
			this.startIndex = indexes[startIndex / pageSize];
		}
	}

	public int getNextIndex()
	{
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= recordCount)
			return getStartIndex();
		else
			return nextIndex;
	}

	public int getPreviousIndex()
	{
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0)
			return 0;
		else
			return previousIndex;
	}

}
